package com.milhouz.findmydroid.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AppOpenHelper extends SQLiteOpenHelper {

	public AppOpenHelper(Context context) {
		super(context, AppConstants.DATABASE_NAME, null, AppConstants.DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + AppConstants.TABLE_NAME_CHARACTERS + " (" //
				+ AppConstants.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," //
				+ AppConstants.COLUMN_NAME + " TEXT," //
				+ AppConstants.COLUMN_DESCRIPTION + " TEXT," //
				+ AppConstants.COLUMN_LANG + " TEXT," //
				+ AppConstants.COLUMN_IS_USER + " INTEGER," //
				+ AppConstants.COLUMN_CATEGORY_ID + " INTEGER );");

		db.execSQL("CREATE TABLE " + AppConstants.TABLE_NAME_CATEGORIES + " (" //
				+ AppConstants.COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," //
				+ AppConstants.COLUMN_NAME + " TEXT," //
				+ AppConstants.COLUMN_IS_USER + " INTEGER);");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Logs that the database is being upgraded
		Log.w(AppConstants.TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
		// Kills the table and existing data
		db.execSQL("DROP TABLE IF EXISTS " + AppConstants.TABLE_NAME_CHARACTERS);
		db.execSQL("DROP TABLE IF EXISTS " + AppConstants.TABLE_NAME_CATEGORIES);
		// Recreates the database with a new version
		onCreate(db);
	}
}
